﻿namespace Pap.Web.Modules.Membership.Views
{
    using System;
    using System.Collections.Generic;
    using Coolite.Ext.Web;
    using global::Utils.UIPermission;
    using Pap.Web.Modules.Membership.ViewModel;
    using Pap.Web.Utils;


    /// <summary>
    /// 
    /// </summary>
    public partial class RolesList : BasePage<RolesListViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewModel != null)
            {
                storeRoles.DataSource = this.ViewModel.Find("");
                storeRoles.DataBind();
            }
        }

        /// <summary>
        /// Handles the Command event of the GrvRoles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void GrvRoles_Command(object sender, AjaxEventArgs e)
        {
            if (e.ExtraParams["CommandName"].Equals("Edit"))
            {
                RowSelectionModel model = grvRoles.SelectionModel.Primary as RowSelectionModel;
                Guid roleID = e.ExtraParams["ID"].ToString().ToGuid();
                //Ext.Redirect(string.Format("~/Membership.route/RoleDetails?RoleID={0}", roleID), GetLocalResourceObject("MSGWorking").ToString());
                this.Redirect("Membership", "RoleDetails", "RoleID=" + roleID.ToString());
            }
        }

        /// <summary>
        /// Deletes the roles.
        /// </summary>
        [AjaxMethod]
        public void DeleteSelectedRoles()
        {
            try
            {
                List<Guid> rolesIDs = new List<Guid>();
                SelectedRowCollection deletedRows = (grvRoles.SelectionModel.Primary as RowSelectionModel).SelectedRows;
                foreach (SelectedRow row in deletedRows)
                {
                    rolesIDs.Add(new Guid(row.RecordID));
                }
                this.ViewModel.DeleteAll(rolesIDs);
                //---------------------------------------------------------------
                storeRoles.DataSource = this.ViewModel.Find("");
                storeRoles.DataBind();
                grvRoles.Reload();
            }
            catch
            {
                Ext.Msg.Configure(new MessageBox.Config
                {
                    Title =
                    GetLocalResourceObject("MBError").ToString(),
                    Message = GetLocalResourceObject("MBCouldntDelete").ToString(),
                    Icon = MessageBox.Icon.ERROR,
                    Buttons = MessageBox.Button.OK
                }).Show();
            }
        }


        #region IPermittedPage Members

        /// <summary>
        /// Gets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            get { return "RolesList"; }
        }

        #endregion
    }
}
